package simpleServerThread;

import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class UserInterface {
	private JFrame fr;
	private JPanel panelButton;
	private JTextField display;
	private JButton startServer, stopServer;
	private Color coloreSfondo;
	private ImageIcon startIcon;
	private ImageIcon stopIcon;
	private SimpleServerApplicationThread serverThread;
	private String serverMessage;
	
	public UserInterface(){
		fr = new JFrame("Geo Commerce Server");
		startIcon = new ImageIcon("Start.png");
		stopIcon = new ImageIcon("Stop.png");
		panelButton = new JPanel();
		display = new JTextField(null,60);
		startServer = new JButton("Start", startIcon);
		stopServer = new JButton("Stop", stopIcon);
		coloreSfondo = new Color(0x0099ff);
	}
	
	public void gui(){
		fr.setSize(350, 200);
		fr.setLayout(new GridLayout(2, 1, 1, 30));
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fr.getContentPane().setBackground(coloreSfondo);
		
		stopServer.setEnabled(false);
		display.setEditable(false);
		
		panelButton.setLayout(new FlowLayout());
		panelButton.setBackground(fr.getContentPane().getBackground());
		
		startServer.addActionListener(new StartServerListener());
		stopServer.addActionListener(new StopServerListener());
		
		fr.add(display);
		panelButton.add(startServer);
		panelButton.add(stopServer);
		fr.add(panelButton);
		fr.setVisible(true);
	}
	
	public class StartServerListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {

			try {
				
				serverThread = new SimpleServerApplicationThread();
				serverMessage = serverThread.startServer();
				
				display.setText(serverMessage);
				
				startServer.setEnabled(false);
				stopServer.setEnabled(true);
			
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
		}
	}	
	
	public class StopServerListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
				
				serverMessage = serverThread.stopServer();

				display.setText(serverMessage);
				
				startServer.setEnabled(true);
				stopServer.setEnabled(false);
		}
	
	}

}
